﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using DataInfo;
using KenLib;
using KenLib.BusinessManager;

public partial class JOINT_frmJ_CompBranchHistoryInput : System.Web.UI.Page
{


    Hashtable ht_ErrosArray;
    

    protected void Page_Load(object sender, EventArgs e)
    {

        ((JOINT_MstPage_JOINT)this.Master).changeTitle("拠点履歴情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_CompBranchHistoryInput");

        if (Session["企業コード_2"] == null || Session["企業コード_2"].ToString() == "")
            Response.Redirect("~/frmD_Login.aspx");

        if (!IsPostBack)
        {
            LoadYearCombobox(); 
            loadErrosToValidationControl();
            
            loadData();
        }
        else
        {
            if (!(Request.Form["_EventTarget"] != null && Request.Form["_EventTarget"] == "myDblclick"))
            {   
                int check;
               
                if (Int32.TryParse(Request.Form["__EVENTARGUMENT"].ToString(), out check))
                {
                    Session["拠点履歴_mode"] ="update";
                    Session["update拠点履歴ID"] = check;
                    loadDataByID(check);

                }
                


            }
        }
        loadJSErrosContent();

    }


    protected void btn_戻る_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_CompBranchInput.aspx");
    }
    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_MCompManager.aspx");
    }
    //Event Button Reset Form
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        resetForm();
        Session["拠点履歴_mode"] = null;
        Session["update拠点履歴ID"] = null;

    }
    //Event Button Submit Form
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {

                拠点履歴 obj_拠点履歴 = new 拠点履歴();
                
                obj_拠点履歴.勤務開始時間 = txt_勤務開始時間.Text;
                obj_拠点履歴.勤務終了時間 = txt_勤務終了時間.Text;
                obj_拠点履歴.昼休み開始時間 = txt_昼休み開始時間.Text;
                obj_拠点履歴.昼休み終了時間 = txt_昼休み終了時間.Text;
                obj_拠点履歴.平均週間労働日数 = txt_平均週間労働日数.Text;
                obj_拠点履歴.拠点ID = Int32.Parse(Session["拠点"].ToString());
                obj_拠点履歴.年度 = Int32.Parse(txt_年度.SelectedValue);
                if (txt_年間労働日数.Text != "") { obj_拠点履歴.年間労働日数 = Double.Parse(txt_年間労働日数.Text); }
                if (txt_年間労働日数翌々年.Text != "") { obj_拠点履歴.年間労働日数翌々年 = Double.Parse(txt_年間労働日数翌々年.Text); }
                if (txt_所定労働時間.Text != "") { obj_拠点履歴.所定労働時間 = Double.Parse(txt_所定労働時間.Text); }
                if (txt_検診回数.Text != "") { obj_拠点履歴.検診回数 = Int32.Parse(txt_検診回数.Text); }
                if (txt_休憩時間.Text != "") { obj_拠点履歴.休憩合計時間 = Int32.Parse(txt_休憩時間.Text); }


                if (txt_検診実施日.Text != "")
                {
                    obj_拠点履歴.検診実施日 = new DateTime(Int32.Parse(txt_検診実施日.Text.Substring(0, 4)), Int32.Parse(txt_検診実施日.Text.Substring(5, 2)), Int32.Parse(txt_検診実施日.Text.Substring(8, 2)));
                }
                else
                {
                    obj_拠点履歴.検診実施日 = null;
                }

                KenLib.BusinessManager.拠点履歴Manager dataMamager = new KenLib.BusinessManager.拠点履歴Manager();

                if (Session["拠点履歴_mode"] != null && Session["update拠点履歴ID"] != null)
                {
                    obj_拠点履歴.ID = Int32.Parse(Session["update拠点履歴ID"].ToString());
                    拠点情報Manager dataManager = new 拠点情報Manager();
                    DataTable data = dataManager.selec拠点情報ByID((int) obj_拠点履歴.拠点ID);
                    if (data.Rows.Count > 0)
                    {
                        obj_拠点履歴.拠点コード = data.Rows[0]["拠点コード"].ToString();
                    }
                    dataMamager.update拠点履歴(obj_拠点履歴);
                    Session["拠点履歴_mode"] = null;
                    Session["update拠点履歴ID"] = null;
                    
                }
                else
                {
                    obj_拠点履歴.廃止区分 = 0;
                    拠点情報Manager dataManager = new 拠点情報Manager();
                    DataTable data = dataManager.selec拠点情報ByID((int)obj_拠点履歴.拠点ID);
                    if (data.Rows.Count > 0)
                    {
                        obj_拠点履歴.拠点コード = data.Rows[0]["拠点コード"].ToString();
                    }
                    dataMamager.insert拠点履歴(obj_拠点履歴);
                   
                    
                }

                resetForm();          
                loadData();
                Response.Redirect("~/JOINT/frmJ_CompBranchHistoryInput.aspx");

        }

    }


    private void loadDataByID(int ID)
    {

        KenLib.BusinessManager.拠点履歴Manager dataManager = new KenLib.BusinessManager.拠点履歴Manager();
        DataTable dt = dataManager.select拠点情報ByID(ID);
        if (dt.Rows.Count > 0)
        {

            txt_年度.SelectedValue = dt.Rows[0]["年度"].ToString();
            txt_勤務開始時間.Text = dt.Rows[0]["勤務開始時間"].ToString();
            txt_勤務終了時間.Text = dt.Rows[0]["勤務終了時間"].ToString();
            txt_昼休み開始時間.Text = dt.Rows[0]["昼休み開始時間"].ToString();
            txt_昼休み終了時間.Text = dt.Rows[0]["昼休み終了時間"].ToString();
            txt_休憩時間.Text = dt.Rows[0]["休憩合計時間"].ToString();
            txt_所定労働時間.Text = dt.Rows[0]["所定労働時間"].ToString();
            txt_平均週間労働日数.Text = dt.Rows[0]["平均週間労働日数"].ToString();
            txt_年間労働日数.Text = dt.Rows[0]["年間労働日数"].ToString();
            txt_年間労働日数翌年.Text = dt.Rows[0]["年間労働日数翌年"].ToString();
            txt_年間労働日数翌々年.Text = dt.Rows[0]["年間労働日数翌々年"].ToString();
            txt_検診回数.Text = dt.Rows[0]["検診回数"].ToString();
            if (dt.Rows[0]["検診実施日"].ToString() != "")
            {
                txt_検診実施日.Text = dt.Rows[0]["検診実施日"].ToString().Substring(0,10);
            }
        }


    }

    //Load Data to GataGrid
    public void loadData()
    {

        KenLib.BusinessManager.拠点履歴Manager dataManager = new KenLib.BusinessManager.拠点履歴Manager();
        DataTable dt = dataManager.Select拠点履歴by拠点ID(Convert.ToInt32(Session["拠点"].ToString()));
        if (dt.Rows.Count > 0)
        {
            grv.DataSource = dt;
            grv.DataBind();
            grv.Columns[4].Visible = false;
        }
        else
        {
            AppCode_Common.ShowEmtyGrid<拠点履歴>(grv, 4, "データがありません。", "");

        }
    }

    //Funtion Clear All Control
    public void resetForm()
    {
        txt_年度.SelectedIndex = -1;
        txt_勤務開始時間.Text = "";
        txt_勤務終了時間.Text = "";
        txt_昼休み開始時間.Text = "";
        txt_昼休み終了時間.Text = "";
        txt_休憩時間.Text = "";
        txt_所定労働時間.Text = "";
        txt_平均週間労働日数.Text = "";
        txt_年間労働日数.Text = "";
        txt_年間労働日数翌年.Text = "";
        txt_年間労働日数翌々年.Text = "";
        txt_検診回数.Text = "";
        txt_検診実施日.Text = "";
        

    }

    //Load Year For Combobox 
    public void LoadYearCombobox() 
    {
        DataTable tempTable = new DataTable();
        tempTable.Columns.Add("Value",typeof(Int32));
        tempTable.Columns.Add("Text",typeof(String));
        for (int startYear = DateTime.Now.Year - 9; startYear <= DateTime.Now.Year; startYear++)
        {

            tempTable.Rows.Add(startYear,startYear.ToString());

        }
        txt_年度.DataSource = tempTable;
        txt_年度.DataTextField = "Text";
        txt_年度.DataValueField = "Value";
        txt_年度.DataBind();
        
    
    }

    //Send Erros Info to JavaScript
    private void loadJSErrosContent()
    {
        //Hashtable hs_Temp = loadErrosContent();
        string er_array = "var  arrayErros = new Array(); var startDate = " + (DateTime.Now.Year - 10).ToString() + " ; var endDate=" + DateTime.Now.Year + ";";

        //foreach (DictionaryEntry entry in hs_Temp)
        //{
        //    er_array = er_array + @" arrayErros[""" + entry.Key + @"""] =""" + entry.Value + @""";";
        //}
        //ht_ErrosArray = hs_Temp;
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>" + er_array + "</script>");
        
    }

    //Get Erros from Database
    private Hashtable loadErrosContent()
    {
        KenLib.BusinessManager.拠点履歴Manager dataManager = new KenLib.BusinessManager.拠点履歴Manager();
        DataTable _dataErrosContens = dataManager.SelectErros();
        Hashtable ht_ErrosCt = new Hashtable();
        foreach (DataRow dr in _dataErrosContens.Rows)
        {
            ht_ErrosCt.Add(dr[0].ToString(), dr[1].ToString());
        }
        return ht_ErrosCt;


    }

    //Add Content Erros to Control Validation
    public void loadErrosToValidationControl()
    {
        ht_ErrosArray = loadErrosContent();
        vld_txt_年度.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        
        vld_txt_勤務開始時間.ErrorMessage = ht_ErrosArray["ER00018"].ToString();
        vld_txt_勤務終了時間.ErrorMessage = ht_ErrosArray["ER00018"].ToString();
        vld_txt_昼休み開始時間.ErrorMessage = ht_ErrosArray["ER00018"].ToString();
        vld_txt_昼休み終了時間.ErrorMessage = ht_ErrosArray["ER00018"].ToString();
        vld_txt_休憩時間.ErrorMessage = ht_ErrosArray["ER00018"].ToString();
        
        vld_txt_所定労働時間.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        vld_txt_平均週間労働日数.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
       
        vld_txt_年間労働日数.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        vld_txt_年間労働日数翌年.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        
        vld_txt_年間労働日数翌々年.ErrorMessage = ht_ErrosArray["ER00002"].ToString();

        vld_txt_所定労働時間.ErrorMessage = ht_ErrosArray["ER00002"].ToString();

        vld_txt_検診回数.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        vld_txt_検診実施日.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        

        vld_txt_勤務終了時間1.ErrorMessage = ht_ErrosArray["ER00002"].ToString();
        vld_txt_昼休み終了時間1.ErrorMessage = ht_ErrosArray["ER00002"].ToString();

        vld_txt_勤務開始時間2.ErrorMessage = ht_ErrosArray["ER00001"].ToString();
        vld_txt_昼休み開始時間2.ErrorMessage = ht_ErrosArray["ER00001"].ToString();

        vld_txt_検診実施日1.ErrorMessage = ht_ErrosArray["ER00005"].ToString();

    }

    //Double CLick GridView
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            e.Row.Attributes.Add("ondblclick", "Javascript:__doPostBack('myDblClick','" + e.Row.Cells[4].Text + "');");
          
           
        }

    }

   
}
